import {
  delLastComma,
  getForeachItemStr,
  leftStr2Arr,
  leftStr2ArrWithDefaultSeparator,
  toCamelCase
} from '@/utils/commonUtil.js'
import { commonConfig } from '@/utils/config.js'
import { ElMessage } from 'element-plus'
import { parseTableInfo } from '@/utils/sqlGenerator.js'

export const convertInsertBatch = (that) => {
  const { leftStr, splitArr } = checkDdlStr(that) ?
    sqlStr2Arr(that) :
    leftStr2Arr(that.leftStr, ',')
  that.leftStr = leftStr
  const columns = splitArr.join(', ')
  that.rightStr = columnToForeach(leftStr, splitArr)
  switch (that.dbType) {
    case 'mysql':
    case 'postgresql':
      that.rightStr = `insert into ${that.tableName} (${columns})\nvalues\n${that.rightStr}`
      break
    case 'oracle':
      that.rightStr =
        `begin;\n${ commonConfig.inForeachPrefix }\ninsert into ${ that.tableName } (${ columns })\nvalue\n(\n${ getForeachItemStr(splitArr) }\n)\n${ commonConfig.suffix }\ncommit;\nend;`
      break
    default:
      ElMessage.error('当前类型暂未支持')
  }
}

const columnToForeach = (leftStr, splitArr) => {
  let resultStr = ''
  if (splitArr.length === 1) {
    resultStr = `${ commonConfig.outForeachPrefix }\t#{item}${ commonConfig.suffix }`
  } else {
    resultStr = `${ commonConfig.inForeachPrefix }(\n${ getForeachItemStr(splitArr) }\n)${ commonConfig.suffix }`
  }
  return resultStr
}
const getForeachStr = (splitArr) => {
  // 分割字符串
  let resultStr = ''
  if (splitArr.length === 1) {
    resultStr = `${ commonConfig.outForeachPrefix }\t#{item}${ commonConfig.suffix }`
  } else {
    resultStr = `${ commonConfig.inForeachPrefix }(\n${ getForeachItemStr(splitArr) }\n)${ commonConfig.suffix }`
  }
  return resultStr
}


function checkDdlStr (that) {
  const regex = /^create table/i
  return regex.test(that.leftStr)
}

function sqlStr2Arr (that) {
  parseTableInfo(that)
  const columnList = that.tableInfo.columns
  that.tableName = that.tableInfo.tableName
  const splitArr = columnList.map(item => item.name)
  return { leftStr: that.leftStr, splitArr }
}


export const convertForeachBatch = (that) => {
  const { leftStr, splitArr } = checkDdlStr(that) ?
    sqlStr2Arr(that) :
    leftStr2ArrWithDefaultSeparator(that.leftStr)
  that.leftStr = leftStr
  const columns = splitArr.join(', ')
  const foreachStr = getForeachStr(splitArr)
  const key = that.primaryKey || splitArr[0]
  switch (that.dbType) {
    case 'mysql':
      that.rightStr = `insert into ${that.tableName} (${columns})\nvalues\n${foreachStr}\nON DUPLICATE KEY UPDATE\n`
      splitArr.map(item => {
        that.rightStr += `${ item }= values(${ item }),\n`
      })
      that.rightStr = delLastComma(that.rightStr)
      break
    case 'postgresql':
      that.rightStr = `insert into ${that.tableName} (${columns})\nvalues\n${foreachStr}\nON CONFLICT (${key})DO UPDATE SET\n`
      splitArr.map(item => {
        if (item !== key) {
          that.rightStr += `${ item }= excluded.${ item },\n`
        }
      })
      that.rightStr = delLastComma(that.rightStr)
      break
    case 'oracle':
      that.rightStr = `begin;\n<foreach collection="list" item="item" separator=";">\nupdate ${ that.tableName }\nset\n`
      splitArr.map(item => {
        if (item !== key) {
          that.rightStr += `${ item }= #{item.${ toCamelCase(item) }},\n`
        }
      })
      that.rightStr = delLastComma(that.rightStr)
      that.rightStr += `\nwhere ${ key }= #{item.${ toCamelCase(key) }};\n</foreach>\ncommit;\nend;`
      break
    default:
      ElMessage.error('当前类型暂未支持')
  }
}
